package com.bdqn.mr.partition;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class FlowMapper extends Mapper<LongWritable, Text, Text, FlowBean> {

    // value
    private final static FlowBean flowBean = new FlowBean();
    // key
    private Text phone = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //  key 每一行的偏移量
        //  value 1	13736230513	192.196.100.1	www.bdqn.com	2481	24681	200

        // 把每一行数据转换为字符串进行处理
        String line = value.toString();
        String[] fields = line.split("\t");

        // 拆分字段，获取想要的数据
        String p = fields[1];
        // 上行流量
        Long up = Long.parseLong(fields[fields.length - 3]);
        // 下行流量
        Long down = Long.parseLong(fields[fields.length - 2]);

        // 构建mapper输出的对象
        phone.set(p);
        flowBean.setUpFlow(up);
        flowBean.setDownFlow(down);
        flowBean.setTotalFlow();

        context.write(phone, flowBean);
    }
}
